import logging  # 引入logging模块
import time
from logging.handlers import TimedRotatingFileHandler

from app import config
# 第一步，创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)  # Log等级总开关


# 第二步，创建一个handler，用于写入日志文件
rq = time.strftime('%Y-%m-%d', time.localtime(time.time()))
logfile = config.log_abspath + rq + '.log'

fh = TimedRotatingFileHandler(filename=logfile, when="D", interval=1, backupCount=15, encoding='utf-8')
fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)  # 输出到cmd的log等级的开关

# 第三步，定义handler的输出格式
fm1 = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(fm1)

fm2 = logging.Formatter("%(levelname)s: %(message)s")
sh.setFormatter(fm2)
# 第四步，将logger添加到handler里面
logger.addHandler(fh)
logger.addHandler(sh)



# # 日志
# logger.debug('this is a logger debug message')
# logger.info('this is a logger info message')
# logger.warning('this is a logger warning message')
# logger.error('this is a logger error message')
# logger.critical('this is a logger critical message')